package com.jk.util.autotest;

import com.jk.util.autotest.model.Environment;
import com.jk.util.autotest.model.InterfaceInfo;
import com.jk.util.autotest.model.InterfaceTestInfo;
import com.jk.util.autotest.util.ExcelInputUtil;
import com.jk.util.autotest.util.ExcelOutputUtil;
import com.jk.util.autotest.util.HttpUtilPlus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author jk
 */
public class Main {
    private static Object block = new Object();
    private static int count;
    public static List<InterfaceTestInfo> interfaceTestInfos = Collections.synchronizedList(new ArrayList<>());
    public static String environment = Environment.ALIYUN;
    public static void main(String[] args) {
        Long start = System.currentTimeMillis();
        try {
            List<InterfaceInfo> interfaceInfos = ExcelInputUtil.readExcel("E:\\target\\线上-山西环境.xls");
            for (InterfaceInfo interfaceInfo : interfaceInfos) {
                //先强制读一次数据库
                HttpUtilPlus.interfaceUtil(environment+interfaceInfo.getAddress(),interfaceInfo.getData(),true);
                Thread thread = new TestThread(interfaceInfo.getName(),environment+interfaceInfo.getAddress(),interfaceInfo.getData());
                thread.start();
            }

            while (true){
                if(count == interfaceInfos.size()){
                    break;
                }
                System.out.println("用时"+((System.currentTimeMillis()-start)/1000)+"s");
                System.out.println(count+"/"+interfaceInfos.size());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            ExcelOutputUtil.writeExcel("E:\\target\\target.xls",interfaceTestInfos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void count(){
        synchronized (block){
            count++;
        }
    }
}
